#encoding: utf-8
from flask import render_template, redirect, url_for, flash, request
from . import auth
from ..forms import RegisterForm, LoginForm
from ..models import User
from flask_login import login_user, logout_user, login_required
from ..email import send_email

# 登录页面
@auth.route("/login", methods=["GET", "POST"])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        email = form.email.data
        password = form.password.data
        # 按照邮箱地址获取用户信息
        user = User.getUserByEmail(email)
        # 判断邮箱地址和密码是否正确
        if user is not None and user.verify_password(password):
            # 判断邮箱地址是否已经验证过
            if user.is_confirmed:
                # 判断用户是否被限制登录
                if user.status:
                    login_user(user, form.remember_me.data)
                    # 重新设置最后登录时间
                    user.setLastLoginedTime()
                    return redirect(request.args.get("next") or url_for("main.index"))
                else:
                    print("------已经限制登录-----")
                    flash({"errorMsg": ["您的账户已被限制登录!"]})
                    return render_template("auth_login.html", form=form)
            else:
                # 生成密令
                token = user.generate_confirmation_token()
                send_email(email=email, user=user, token=token)
                return redirect(url_for("auth.activate_email", email=email))
        else:
            # 如果邮箱地址或密码错误，则提示错误信息
            flash({"errorMsg": ["用户名或密码不正确！"]})
            return render_template("auth_login.html", form=form)
    else:
        print(form.errors)
        # 提示错误信息
        flash(form.errors)
    return render_template("auth_login.html", form=form)

# 注册页面
@auth.route("/register", methods=["GET", "POST"])
def register():
    form = RegisterForm()
    # 验证数据是否合法
    if form.validate_on_submit():
        username = form.username.data
        email = form.email.data
        password = form.password.data
        confirmPassword = form.confirmPassword.data
        # 重定向到登录页面
        print("----liuwenchao----")
        # 判断username是否重复
        is_username_existed = User.is_username_existed(username)
        if is_username_existed:
            flash({"username":["用户名已经存在！"]})
            return render_template("auth_register.html", form=form)

        # 判断email是否重复
        is_email_existed = User.is_email_existed(email)
        if is_email_existed:
            flash({"email": ["邮箱已经存在！"]})
            return render_template("auth_register.html", form=form)

        # 添加
        user = User(username=username, email=email, password=password)
        user.add()

        # 生成密令
        token = user.generate_confirmation_token()
        send_email(email=email, user=user, token=token)
        return redirect(url_for("auth.activate_email", email=email))
    else:
        print(form.errors)
        # 错误提示
        flash(form.errors)
    return render_template("auth_register.html", form=form)

# 退出登录
@auth.route("/logout")
@login_required
def logout():
    logout_user()
    return redirect(url_for("main.index"))

# 验证邮件
@auth.route("/confirm/<token>")
def confirm(token):
    user = User.getUserByConfirmToken(token)
    # 判断user是否存在， 如果存在则进行登录
    if user:
        login_user(user)
    return render_template("auth_register_result.html", user=user)

# 注册成功
@auth.route("/activate/<email>")
def activate_email(email):
    # 判断数据库中email是否存在
    is_email_existed = User.is_email_existed(email)
    if not is_email_existed:
        return redirect(url_for("main.index"))
    return render_template("activate_email.html", email=email)